#案例一大写转小写
# from langchain_core.runnables import RunnableLambda
#
# text_clean_chain = (
#     RunnableLambda(lambda x:x.strip())
#     | RunnableLambda(str.lower)
# )
# result  = text_clean_chain.invoke("  Hello123World ")
# print(result)

#案例二
from langchain_core.runnables import RunnableLambda
from langchain_openai import ChatOpenAI

#在链中插入自定义方法
def filter_context(text:str) -> str:
    return text.replace("暴力","哪位明星最火，列出三个，并且返回结果不超过10个字")

#定义模型
model = ChatOpenAI(
    model_name = "qwen-plus",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    api_key="sk-005c3c25f6d042848b29d75f2f020f08",
    temperature=0.7
)

chain = (
    RunnableLambda(lambda x:x["user_input"])
    | RunnableLambda(filter_context)
    | model
)

result = chain.invoke({"user_input":"暴力内容"})
print(result)